import {Module} from "./framework.js"
import Page from "./page/page.js"
//import * as PageEvent from "./page/page-event.js"
import Sidebar from "./sidebar/sidebar.js"
import * as SidebarEvent from "./sidebar/sidebar-event.js"
import Property from "./property/property.js"
//import * as PropertyEvent from "./property/property-event.js"
import Search from "./search/search.js"
//import * as SearchEvent from "./search/search-event.js"
//import Setting from "./settings/setting.js"
//import * as SettingEvent from "./settings/setting-event.js"
//import Breadcrumb from "./breadcrumb.js"
import DataStorage from "./data-storage.js"
//import {ConfirmMessager, MessagerLevel} from './messager.js'
//import {proxy_Archive, proxy_Log} from './server-proxy.js';
//import Layout from './layout.js';

/*
window.addEventListener("beforeunload", function (event) {
    var confirmationMessage = "sssssssssssss";

    (event || window.event).returnValue = confirmationMessage;     //Gecko + IE
    return confirmationMessage;
});
*/

///====================================================================================================
///
/// runtime data;
///
///----------------------------------------------------------------------------------------------------

class Application extends Module{
    constructor() {
        super();
        this.sidebar = undefined;
        this.page = undefined;
        this.property = undefined;
        this.search = undefined;
        //this.crumb = undefined;
        this.Init();
    }

    Init() {
        this.sidebar = new Sidebar(this._dispatcher);
        this.page = new Page(this._dispatcher);
        this.property = new Property(this._dispatcher);
        this.search = new Search(this._dispatcher);
        //this.setting = new Setting(this._dispatcher);
        //this.crumb = new Breadcrumb();

        ///TODL article list;
        /*
        this.crumb.AppendListener(Breadcrumb.EVT_BREADCRUMB_CLICKED, (arrParam) => {
            const _SUID = arrParam[0];
            DataStorage.FetchPageContent(_SUID);
            this.sidebar.Select(_SUID);
        });
        */
        /*
        this.sidebar.AppendListener(SidebarEvent.EVT_NOTE_CLICKED, (arrParam) => {
            const _SUID = arrParam[0];
            DataStorage.FetchPageContent(_SUID);
            this.sidebar.Select(_SUID);
        });
        this.sidebar.AppendListener(SidebarEvent.EVT_NEW_NOTE_CLICKED, () => {
            this.property.Update(Property.ProperyMode.MODE_CREATE);
            this.property.Show();
        });
        this.sidebar.AppendListener(SidebarEvent.EVT_SETTINGS_CLICKED, () => {
            this.setting.Show();
        });
        this.setting.AppendListener(SettingEvent.EVT_SETTING_ARCHIVE, () => {
            proxy_Archive()
                .then(() => {
                    console.log("index.js archived");
                });
        });
        this.setting.AppendListener(SettingEvent.EVT_SETTING_LOG, () => {
            proxy_Log()
                .then((data) => {
                    console.log(data);
                });
        });
        this.search.AppendListener(SearchEvent.EVT_SEARCH_RESULT_SELECTED, (arrParam) => {
            const _SUID = arrParam[0];
            DataStorage.FetchPageContent(_SUID);
            this.sidebar.Select(_SUID);
            this.search.Hide();
        });
        this.property.AppendListener(PropertyEvent.EVT_PROPERTY_DELETE_BTN_CLICKED, (arrParam) => {
            this.property.Hide();
            const _SUID = arrParam[0];
            ConfirmMessager.Show("Confirm", 'Are you sure to delete?')
                .then((result) => {
                    if(result) DataStorage.DeletePage(_SUID);
                });
        });
        this.page.AppendListener(PageEvent.EVT_PAGE_PROPERTY_BTN_CLICKED, (arrParam) => {
            const _SUID = arrParam[0];
            const _meta = DataStorage.GetPageProperty(_SUID);
            this.property.Update(Property.ProperyMode.MODE_MODIFY, _meta);
            this.property.Show();
        });
        this.page.AppendListener(PageEvent.EVT_PAGE_CREATE_BTN_CLICKED, (arrParam) => {
            const _SUID = arrParam[0];
            const _meta = DataStorage.GetPageProperty(_SUID);
            this.property.Update(Property.ProperyMode.MODE_CREATE, _meta);
            this.property.Show();
        });

        */
        /*
        const _that = this;
        document.onkeydown = function (event) {
            if (event.ctrlKey || event.metaKey) {
                switch (event.key.toLowerCase()) {
                    case '/':
                        _that.search.Show();
                        event.preventDefault();
                        break;
                    case 'n':
                        _that.property.Update(Property.ProperyMode.MODE_CREATE);
                        _that.property.Show();
                        event.preventDefault();
                        break;
                    default:
                        break;
                }
            }
        };
        */
        DataStorage.Init();

    }
}

let a = new Application();
